package com.zqh.irmadscore.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zqh.irmadscore.service.FailureItemService;
import com.zqh.irmadscore.service.MainItemService;
import com.zqh.irmadscore.mapper.DataItemMapper;
import com.zqh.irmadscore.mapper.FailureItemMapper;
import com.zqh.irmadscore.mapper.MainItemMapper;
import com.zqh.irmadscore.model.dto.MainItemDTO;
import com.zqh.irmadscore.model.entity.DataItem;
import com.zqh.irmadscore.model.entity.MainItem;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MainItemServiceImpl extends ServiceImpl<MainItemMapper, MainItem> implements MainItemService {


    private final DataItemMapper dataItemMapper;

    @Override
    public void saveMainItemWithDataItems(MainItemDTO dto) {

        // 保存MainItem
        MainItem mainItem = dto.getMainItem();

        baseMapper.insert(mainItem);
        // 获取生成的主键ID
        Integer mainItemId = mainItem.getId();

        // 保存关联的DataItems
        List<DataItem> dataItems = dto.getDataItems();

        if (CollUtil.isNotEmpty(dataItems)) {

            dataItems.forEach(item -> item.setMainItemId(mainItemId));

            // 批量插入（需确保DataItemMapper有对应方法）
            dataItemMapper.insertBatch(dataItems);

            System.out.println("保存成功");
        }

    }

    @Override
    public MainItemDTO getMainItemWithDataItems(Integer id) {

        // 查询MainItem
        MainItem mainItem = baseMapper.selectById(id);

        if (mainItem == null) return null;

        // 查询关联的DataItems
        QueryWrapper<DataItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("main_item_id", id);

        List<DataItem> dataItems = dataItemMapper.selectList(queryWrapper);

        MainItemDTO mainItemDTO = MainItemDTO.builder()
                .mainItem(mainItem)
                .dataItems(dataItems)
                .build();


        return mainItemDTO;
    }

    @Override
    public void updateMainItemWithDataItems(MainItemDTO dto) {

        MainItem mainItem = dto.getMainItem();
        // 更新MainItem
        baseMapper.updateById(mainItem);
        Integer mainItemId = mainItem.getId();
        // 删除旧DataItems
        QueryWrapper<DataItem> wrapper = new QueryWrapper<>();
        wrapper.eq("main_item_id", mainItemId);
        dataItemMapper.delete(wrapper);
        // 插入新DataItems
        List<DataItem> dataItems = dto.getDataItems();

        if (CollUtil.isNotEmpty(dataItems)) {
            dataItems.forEach(item -> item.setMainItemId(mainItemId));
            dataItemMapper.insertBatch(dataItems);
        }

    }
    private final FailureItemMapper failureItemMapper;
    private final FailureItemService failureItemService;

//    @Override
//    public List<FailureItem> getHistory() {

//
//        LambdaQueryWrapper<FailureItem> queryWrapper = new LambdaQueryWrapper<>();
//
//       List<FailureItem> failureItemList =  failureItemMapper.selectList(queryWrapper);
//
////        // 收集FailureItemDTO列表
//        List<FailureItemDTO> failureItemDTOList = CollUtil.newArrayList();
//        for (FailureItem failureItem : failureItemList) {
//
////            Integer mainItemId = failureItem.getTractId();
////            MainItem mainItem = baseMapper.selectById(mainItemId);
//
//            FailureItemDTO failureItemDTO = FailureItemDTO.builder()
//                    .failureType(failureItem.getFailureType())
//                    .failureTime(failureItem.getFailureTime())
//                    .id(failureItem.getId())
//                    .build();
//            failureItemDTOList.add(failureItemDTO);
//        }

//        List<FailureItem> failureItemList = failureItemService.list();

//        return null;
//    }

    @Override
    public List<MainItemDTO> getAllMainItem() {

        List<MainItem> mainItemList = baseMapper.selectList(null);

        List<MainItemDTO> mainItemDTOList = CollUtil.newArrayList();


        for(MainItem mainItem : mainItemList)
        {
            Integer id = mainItem.getId();


            // 查询关联的DataItems
            QueryWrapper<DataItem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("main_item_id", id);

            List<DataItem> dataItems = dataItemMapper.selectList(queryWrapper);

            MainItemDTO mainItemDTO = MainItemDTO.builder()
                    .mainItem(mainItem)
                    .dataItems(dataItems)
                    .build();

            mainItemDTOList.add(mainItemDTO);

        }


        return mainItemDTOList;
    }
}
